package com.mall4jcola.cloud.auth;

import com.alibaba.cola.dto.SingleResponse;
import com.mall4jcola.cloud.api.UserAuthsServiceI;
import com.mall4jcola.cloud.auth.executor.AuthsTokenCreateCmdExe;
import com.mall4jcola.cloud.auth.executor.UserLoginInfoUpdateCmdExe;
import com.mall4jcola.cloud.auth.executor.query.AccountVerificationQryExe;
import com.mall4jcola.cloud.auth.executor.query.SingleAccountQryExe;
import com.mall4jcola.cloud.auth.executor.query.SingleLoginInfoQryExe;
import com.mall4jcola.cloud.auth.model.UserLoginInfo;
import com.mall4jcola.cloud.dto.UserAuthsVerifyQry;
import com.mall4jcola.cloud.dto.data.UserAuthsTokenDTO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 认证服务类
 *
 * @author hmxchen
 */
@Service
@AllArgsConstructor
public class UserAuthsServiceImpl implements UserAuthsServiceI {


    private final AccountVerificationQryExe accountVerificationQryExe;
    private final SingleAccountQryExe singleAccountQryExe;
    private final SingleLoginInfoQryExe singleLoginInfoQryExe;
    private final UserLoginInfoUpdateCmdExe userLoginInfoUpdateCmdExe;
    private final AuthsTokenCreateCmdExe authsTokenCreateCmdExe;

    @Override
    public SingleResponse<UserAuthsTokenDTO> authAccount(UserAuthsVerifyQry query) {
        SingleResponse<String> stringSingleResponse = singleAccountQryExe.execute(query.getIdentityType(), query.getIdentifier());
        SingleResponse<UserLoginInfo> userAuthEntitySingleResponse = singleLoginInfoQryExe.execute(query.getIdentityType(),stringSingleResponse.getData());
        UserLoginInfo loginInfo = userAuthEntitySingleResponse.getData();
        accountVerificationQryExe.execute(query.getIdentityType(),query.getCredential(),loginInfo);
        userLoginInfoUpdateCmdExe.execute(loginInfo.getUserAuthEntity().getUaId());
        return authsTokenCreateCmdExe.execute(loginInfo);
    }
}
